import 'package:bruno/bruno.dart';
import 'package:collection/collection.dart';
import 'package:dart_date/dart_date.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/yt_table/yt_profile_table.dart';
import 'package:yt_devops/pages/devices/device_controll/device_controll_utils.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/enums.dart';
import 'package:yt_devops/utils/func_utils.dart';
import 'rate_setting_controller.dart';
import 'rate_setting_model.dart';

class RateSetting extends GetView<RateSettingController> {
  int deviceId;
  ConnectStatusType? connectStatus;
  bool? passEditAuth;
  bool? isEdit;
  String tag = '';

  @override
  late RateSettingController controller;

  RateSetting(
      {Key? key,
      required this.deviceId,
      this.connectStatus,
      this.passEditAuth = false,
      this.isEdit = false,
      required this.tag})
      : super(key: key) {
    controller = Get.put(RateSettingController(), tag: tag);
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width,
      child: GetBuilder<RateSettingController>(
        tag: tag,
        initState: (state) => {controller.initData(deviceId, isEdit!)},
        builder: (_) => Column(
          children: [
            !isEdit!
                ? DeviceControllUtils.createHead(
                    FuncUtils.formatMessage(
                        resKey: ResKey.rateSetting, defaultMessage: '费率设置'),
                    isShowSetButton: true,
                    taphandler: () {
                      controller.goSetting();
                    },
                    buttonDisable: !passEditAuth! ||
                        connectStatus == ConnectStatusType.off,
                  )
                : Container(
                    margin: EdgeInsets.only(top: 10.h),
                  ),
            Container(
              decoration: const BoxDecoration(color: Colour.white),
              margin: EdgeInsets.only(bottom: 10.h),
              child: Column(
                children: [
                  BrnBaseTitle(
                    titleWidget: Container(
                      padding: const EdgeInsets.only(
                        left: 16,
                      ),
                      child: Text(
                        FuncUtils.formatMessage(
                            resKey: ResKey.timeZoneTable,
                            defaultMessage: '时区表'),
                        style: TextStyle(
                          color: Colour.mainTextColor,
                          fontWeight: FontWeight.bold,
                          fontSize: 16.sp,
                        ),
                      ),
                    ),
                  ),
                  YtProfileTable<TimeZoneData>(
                    tableWidth: MediaQuery.of(context).size.width - 32.w,
                    dataSource: controller.timeZoneData,
                    columns: isEdit!
                        ? RateSettingModel.getTimeZoneForm(
                            (String type, int index) =>
                                controller.selectColClick(type, 0, index),
                            (String type, int index) =>
                                controller.timeColClick(type, 0, index),
                          )
                        : RateSettingModel.timeZone,
                  ),
                  ExpansionPanelList(
                    expandedHeaderPadding: EdgeInsets.all(0),
                    expansionCallback: (index, expand) {
                      controller.changeExpanded(index, expand);
                    },
                    animationDuration: Duration(seconds: 1),
                    children: controller.timePeriodTable
                        .mapIndexed((tableIndex, table) {
                      return ExpansionPanel(
                        headerBuilder: (context, isPand) => BrnBaseTitle(
                          titleWidget: Container(
                            padding: const EdgeInsets.only(
                              left: 16,
                            ),
                            child: Text(
                              '${FuncUtils.formatMessage(resKey: ResKey.timePeriodTable, defaultMessage: '时段表')}${tableIndex + 1}',
                              style: TextStyle(
                                color: Colour.mainTextColor,
                                fontWeight: FontWeight.bold,
                                fontSize: 16.sp,
                              ),
                            ),
                          ),
                        ),
                        body: YtProfileTable<TimePeriodData>(
                          tableWidth: MediaQuery.of(context).size.width - 32.w,
                          dataSource: table,
                          columns: isEdit!
                              ? RateSettingModel.getTimePeriodForm(
                                  (String type, int index) => controller
                                      .selectColClick(type, tableIndex, index),
                                  (String type, int index) => controller
                                      .timeColClick(type, tableIndex, index),
                                )
                              : RateSettingModel.timePeriod,
                        ),
                        canTapOnHeader: true,
                        isExpanded: controller.expandedAll[tableIndex],
                      );
                    }).toList(),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
